Consul 作为服务发现/注册中心

  • 服务注册

    1
    待补充
  • 服务发现

    1
    待补充

Consul 作为配置中心(KV)

  • Consul 命令行 KV 存储

    1
    2
    3
    4
    5
    ./consul kv put foo foo_value 
    ./consul kv get foo

    ./consul kv put foo/user1/name charles
    ./consul kv get foo/user1/name
  • Consul Rest API KV 存储

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    curl -X PUT -d "foo_value" http://localhost:8500/v1/kv/foo -v
    * Trying ::1...
    * TCP_NODELAY set
    * Connection failed
    * connect to ::1 port 8500 failed: Connection refused
    * Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connected to localhost (127.0.0.1) port 8500 (#0)
    > PUT /v1/kv/foo HTTP/1.1
    > Host: localhost:8500
    > User-Agent: curl/7.54.0
    > Accept: */*
    > Content-Length: 5
    > Content-Type: application/x-www-form-urlencoded
    >
    * upload completely sent off: 5 out of 5 bytes
    < HTTP/1.1 200 OK
    < Content-Type: application/json
    < Vary: Accept-Encoding
    < Date: Wed, 27 Feb 2019 14:59:21 GMT
    < Content-Length: 5
    <
    true
    curl http://localhost:8500/v1/kv/foo
    [
    {
    "LockIndex": 0,
    "Key": "foo",
    "Flags": 0,
    "Value": "Y2hhcmxlczMz",
    "CreateIndex": 12,
    "ModifyIndex": 14
    }
    ]

    curl -X PUT -d "charles" http://localhost:8500/v1/kv/foo/user1/name
    true
    curl http://localhost:8500/v1/kv/foo/user1/name
    [
    {
    "LockIndex": 0,
    "Key": "foo/user1/name",
    "Flags": 0,
    "Value": "Y2hhcmxlcw==",
    "CreateIndex": 148,
    "ModifyIndex": 148
    }
    ]
  • Consul KV 存储RestAPI 获取 value (Base64编码)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    curl http://localhost:8500/v1/kv/foo -v
    * Trying ::1...
    * TCP_NODELAY set
    * Connection failed
    * connect to ::1 port 8500 failed: Connection refused
    * Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connected to localhost (127.0.0.1) port 8500 (#0)
    > GET /v1/kv/foo HTTP/1.1
    > Host: localhost:8500
    > User-Agent: curl/7.54.0
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Content-Type: application/json
    < Vary: Accept-Encoding
    < X-Consul-Index: 95
    < X-Consul-Knownleader: true
    < X-Consul-Lastcontact: 0
    < Date: Wed, 27 Feb 2019 15:01:58 GMT
    < Content-Length: 168
    <
    [
    {
    "LockIndex": 0,
    "Key": "foo",
    "Flags": 0,
    "Value": "Zm9vX3ZhbHVl",
    "CreateIndex": 17,
    "ModifyIndex": 95
    }
    ]
  • Consul Template 获取 value

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    vi kv.tpl
    {{ key "foo" }}

    ./consul-template -template "./kv.tpl:./kv.result" -once
    cat kv.result
    foo_value


    ---------------------------------------------------------------------------
    动态生成文件:
    $ consul-template \
    -consul 127.0.0.1:8500 \
    -template "/tmp/template.ctmpl:/var/www/nginx.conf:service nginx restart" \
    //输出到www/nginx.conf上,如果nginx.conf发生变化,nginx服务重启
    -retry 30s \ //如果consul服务器不可用,每30s轮询一次服务器
    -once

    $ consul-template \
    -consul-addr=consul:8500 \
    -template "/opt/verynginx/verynginx/consul-templates/in_http_block.ctmpl:/opt/verynginx/verynginx/nginx_conf/in_http_block.conf:sv hup nginx" \
    -template "/opt/verynginx/verynginx/consul-templates/in_server_block.ctmpl:/opt/verynginx/verynginx/nginx_conf/in_server_block.conf:sv hup nginx"


    使用ssl链接
    $ consul-template \
    -consul 127.0.0.1:8543 \
    -ssl \
    -ssl-cert /path/to/client/cert.pem \
    -ssl-ca-cert /path/to/ca/cert.pem \
    -template "/tmp/template.ctmpl:/tmp/result" \
    -dry \ //不知是做啥的
    -once
  • Java 获取所有 KV

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import com.ecwid.consul.v1.ConsulClient;
    import com.ecwid.consul.v1.kv.model.GetValue;
    ...

    ConsulClient consulClient = new ConsulClient("127.0.0.1", 8500);
    List<GetValue> kvList = consulClient.getKVValues("").getValue();
    for (GetValue getValue : kvList) {
    System.out.println(String.format("%s = %s", getValue.getKey(), getValue.getDecodedValue()));
    }
    结果:
    foo=charles33
    foo/user1/name=charles
    foo1=charles33
    foo2=charles33
  • consul-template 获取所有 KV(深度受限于ls后的参数,只扫描指定目录,子目录不会获取)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    vi kvs.tpl
    { {{range ls "/"}}
    * {{ .Key }} = {{ .Value }} {{ end }}
    }

    ./consul-template -template "./kvs.tpl:./kvs.result" -once

    cat kvs.result
    {
    * foo = charles33
    * foo1 = charles33
    * foo2 = charles33
    }

    vi kvs1.tpl
    { {{range ls "/foo/user1/"}}
    * {{ .Key }} = {{ .Value }} {{ end }}
    }

    ./consul-template -template "./kvs1.tpl:./kvs1.result" -once

    cat kvs1.result
    {
    * name = charles
    }
  • RestAPI 获取所有 KV

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    $ curl http://127.0.0.1:8500/v1/kv/?recurse -v
    * Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 8500 (#0)
    > GET /v1/kv/?recurse HTTP/1.1
    > Host: 127.0.0.1:8500
    > User-Agent: curl/7.54.0
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Content-Type: application/json
    < Vary: Accept-Encoding
    < X-Consul-Index: 60
    < X-Consul-Knownleader: true
    < X-Consul-Lastcontact: 0
    < Date: Thu, 28 Feb 2019 01:41:31 GMT
    < Content-Length: 500
    <
    [
    {
    "LockIndex": 0,
    "Key": "foo",
    "Flags": 0,
    "Value": "Y2hhcmxlczMz",
    "CreateIndex": 12,
    "ModifyIndex": 14
    },
    {
    "LockIndex": 0,
    "Key": "foo/user1/name",
    "Flags": 0,
    "Value": "Y2hhcmxlcw==",
    "CreateIndex": 148,
    "ModifyIndex": 148
    },
    {
    "LockIndex": 0,
    "Key": "foo1",
    "Flags": 0,
    "Value": "Y2hhcmxlczMz",
    "CreateIndex": 59,
    "ModifyIndex": 59
    },
    {
    "LockIndex": 0,
    "Key": "foo2",
    "Flags": 0,
    "Value": "Y2hhcmxlczMz",
    "CreateIndex": 60,
    "ModifyIndex": 60
    }
    ]
  • 更多资料详见 https://github.com/hashicorp/consul-template